import _ from 'lodash';

/**
 * 返回首次 value 在数组array中被找到的 索引值
 * @param array
 * @param item
 * @returns {number}
 */
export const indexOf = (array, value) => {
    let index = -1;
    if (array && _.isArray(array)) {
        for (let i = 0; i < array.length; i++) {
            if (array[i] == value) {
                return i;
            }
        }
    }
    return index;
}


/**
 * 是否是空白字符串
 *
 * @param value
 * @returns {boolean}
 */
export const isBlank = (value) => {
    return value == undefined || value.trim() == "";
}

/**
 * 解析整数
 *
 * @param value
 * @param defaultVal
 * @returns {number}
 */
export const parseIntValue = (value, defaultVal) => {
    let parsed = parseInt(value);
    return isNaN(parsed) ? defaultVal : parsed;
}

/**
 * 添加样式,解决编译后文件弹框样式丢失问题
 */
export const addEngineStyle = () => {
    let parentWin = top.window;
    if (parentWin != undefined) {
        let parentIndex4singleCssItem = jQuery("link[href='/spa/contractmanagement/static/index4single.css']", parentWin.document);
        if (parentIndex4singleCssItem == undefined && parentIndex4singleCssItem.length == 0) {
            jQuery("head", parentWin.document).append('<link rel="stylesheet" type="text/css" href="/spa/contractmanagement/static/index4single.css">');
        }
        let parentIndexCssItem = jQuery("link[href='/spa/contractmanagement/static/index.css']", parentWin.document);
        if (parentIndexCssItem == undefined || parentIndexCssItem.length == 0) {
            jQuery("head", parentWin.document).append('<link rel="stylesheet" type="text/css" href="/spa/contractmanagement/static/index.css">');
        }
    }
}

/**
 * undefined或者null转空白字符串
 *
 * @param obj
 * @returns {string}
 */
export const null2String = (obj) => {
    return obj === undefined || obj === null ? "" : obj;
}

/**
 * 获取时间戳
 */
export const timestamp = () => {
    let time = new Date().getTime()
}

/**
 * 是否为合同的url地址
 *
 * @param url
 * @returns {boolean}
 * @constructor
 */
export const isURL = (url) => {
    const strRegex = "^((https|http)?://)"
        + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
        + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
        + "|" // 允许IP和DOMAIN（域名）
        + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
        + "[a-z]{2,6})" // first level domain- .com or .museum
        + "(:[0-9]{1,4})?" // 端口- :80
        + "((/?)|" // a slash isn't required if there is no file name
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
    var re = new RegExp(strRegex);
    //re.test()
    if (re.test(url)) {
        return (true);
    } else {
        return (false);
    }
}